package grp.test.controller;

import grp.test.common.ServerResponse;
import grp.test.common.Const;
import grp.test.common.ResponseCode;
import grp.test.common.TokenCache;

import grp.test.pojo.Book;
import grp.test.pojo.User;
import grp.test.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/book/")
public class BookController {
    @Autowired
    private IBookService iBookService;

    @RequestMapping(value = "bookList.do")
    @ResponseBody
    public ServerResponse<List<Book>> bookList(HttpSession session){
        User currentUser =(User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"管理员未登录,需要强制登录！");
        }
        else if (currentUser.getUserType()!= Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("非管理员，无法查看已上架书籍列表");
        }
        return iBookService.bookList();
    }

    @RequestMapping(value = "book_add.do")
    @ResponseBody
    public ServerResponse<Book> book_add(HttpServletRequest request, HttpSession session)
    {
        User currentUser =(User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"管理员未登录,需要强制登录！");
        }
        else if (currentUser.getUserType()!= Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("非管理员，不能上架书籍");
        }
        Integer bookId =Integer.valueOf(request.getParameter("bookId"));
        String bookName = request.getParameter("bookName");
        System.out.println(request.getCharacterEncoding());
        Double bookPrice = Double.valueOf(request.getParameter("bookPrice"));
        Integer bookStore = Integer.valueOf(request.getParameter("bookStore"));
        System.out.println(request.getParameter("bookDes"));
        String bookDes = request.getParameter("bookDes");
        Integer bookType = Integer.valueOf(request.getParameter("bookType"));
        Book newBook =new Book(bookId,bookName,bookPrice,bookStore,bookDes,bookType);
        return iBookService.book_add(newBook);
    }

    @RequestMapping(value = "book_delete.do")
    @ResponseBody
    public ServerResponse<String> book_delete(HttpServletRequest request, HttpSession session){
        User currentUser =(User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"管理员未登录,需要强制登录！");
        }
        else if (currentUser.getUserType()!= Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("非管理员，不能下架书籍");
        }
        Integer bookId;
        if(request.getParameter("deleteBook")!=null)
        {
            bookId = Integer.valueOf(request.getParameter("deleteBook"));
        }
        else bookId = 0;
        System.out.println(bookId);
        return iBookService.book_delete(bookId);
    }
}
